package com.lsh.oms.core.records;

import com.lsh.base.common.model.CommonResult;
import com.lsh.base.lang.BigDecimals;
import com.lsh.base.lang.DateTimes;
import com.lsh.base.lang.Maps;
import com.lsh.base.lang.json.Jsons;
import com.lsh.ofc.api.dto.ReturnDetailDTO;
import com.lsh.ofc.api.dto.ReturnHeadDTO;
import com.lsh.ofc.api.service.ReturnRpcService;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.records.core.Return;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.lsh.base.lang.BigDecimals.divideWithScale4;
import static com.lsh.base.lang.BigDecimals.multiplyWithScale4;
import static com.lsh.base.lang.bean.Beans.*;

public class ReplaceResult extends ReturnResultRecord {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReplaceResult.class);

    @Resource
    private ReturnRpcService ofc;

    @Override
    public void paymentNotify(Map<String, Object> request) {
        throw BusinessException.unsupportedOperation();
    }

    @Override
    public void run(Map<String, Object> request) {

        ReturnHeadDTO requestToOfc = new ReturnHeadDTO();

        requestToOfc.setReturnCode(returnId);
        requestToOfc.setReturnOrderType(Return.Occasion.BEFORE_SIGN.name().equals(getString(ext, "return.occasion")) ? 1 : 2);
        requestToOfc.setOrderCode(orderId);
        requestToOfc.setRegionCode(getInt(ext, "return.zone.id"));
        requestToOfc.setAddressCode(getLong(ext, "return.address.id"));
        requestToOfc.setWarehouseCode(getString(ext, "return.warehouse.id"));
        requestToOfc.setWarehouseName(getString(ext, "return.warehouse.name"));
        requestToOfc.setCreateTime(DateTimes.seconds(createdAt));
        requestToOfc.setOrderAmount(amount);
        requestToOfc.setCount(1);
        requestToOfc.setVenderId(venderId);

        requestToOfc.setDetails(details.stream().map($ -> ((ReturnResultRecord.ReturnResultDetailRecord) $)).map($ -> {
            ReturnDetailDTO item = new ReturnDetailDTO();
            item.setDetailId($.id);
            item.setGoodsCode($.skuId);
            item.setGoodsName($.skuName);
            item.setGoodsSaleUnit($.saleUnit);
            item.setGoodsPrice($.price);
            item.setGoodsQty($.quantity());
            item.setGoodsAmount($.amount());
            item.setSkuCode(getLong($.ext, "goods.item_id"));
            item.setSkuQty($.realQuantity());
            return item;
        }).collect(Collectors.toList()));

        LOGGER.info("push return: {}, to ofc: {}", id, Jsons.toJsonString(requestToOfc));
        CommonResult<List<String>> response = ofc.createRo(requestToOfc);
        LOGGER.info("push return: {}, response from ofc: {}", id, Jsons.toJsonString(response));
        List<String> returnSoIds = response.getData();
        this.soInfo = Maps.newHashMap("ids", returnSoIds);
        mapper.updateSoInfo(this);
    }
}
